package com.example.java.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.java.entity.Product;
import com.example.java.entity.Productclass;
;
import com.example.java.mapper.ProductMapper;
import com.example.java.mapper.ProductclassMapper;
import com.example.java.utils.QiniuImageUtils;
import com.example.java.utils.Result;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


import java.util.List;
import java.util.Map;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductclassMapper productclassMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private QiniuImageUtils qiniuImageUtils;

    /**
     * 返回分类
     *
     * @return
     */
    @RequestMapping(value = "/class", method = RequestMethod.POST)
    public Result product_class(@RequestBody Map<String, Integer> map) {
        System.out.println(map);
        QueryWrapper<Productclass> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("classpower");
        if (map.containsKey("limit") && (int) map.get("limit") != 0) {
            queryWrapper.last("limit " + map.get("limit"));
        }
        List<Productclass> list = productclassMapper.selectList(queryWrapper);

        for (Productclass productclass : list) {
            productclass.setClasspic(qiniuImageUtils.generatePrivateImageUrl(productclass.getClasspic()));
        }

        return Result.ok().data("data", list);
    }


    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Result product_list(@RequestBody Map<String, Integer> map) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        if (map.containsKey("limit") && map.get("limit") != 0) {
            queryWrapper.last("limit 6");
        }
        queryWrapper.orderByDesc("top").orderByDesc("id");
        List<Product> productList = productMapper.selectList(queryWrapper);

        for (Product product: productList){
            product.setPic(qiniuImageUtils.generatePrivateImageUrl(product.getPic()));
        }

        return Result.ok().data("data", productList);
    }

    /**
     * 转换图片真是的地址
     * @param content
     * @return
     */
    public String convertRelativePathToFullUrl(String content) {
        // 使用正则表达式匹配<img>标签中的相对路径
        String pattern = "<img\\s+src=\"(.*?)\"";
        Pattern imgPattern = Pattern.compile(pattern);
        Matcher matcher = imgPattern.matcher(content);
        // 替换相对路径为完整的图片地址
        StringBuffer result = new StringBuffer();
        while (matcher.find()) {
            String relativePath = matcher.group(1);
            String fullUrl = qiniuImageUtils.generatePrivateImageUrl(relativePath);
            matcher.appendReplacement(result, Matcher.quoteReplacement("<img src=\"" + fullUrl + "\""));
        }
        matcher.appendTail(result);
        return result.toString();
    }



    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    public Result product_detail(@RequestBody Product product) {
        try {
            QueryWrapper<Product> proQueryWrapper = new QueryWrapper<>();
            proQueryWrapper.eq("id", product.getId());
            Product product1 = productMapper.selectById(product.getId());
            product1.setPic(qiniuImageUtils.generatePrivateImageUrl(product1.getPic()));

            product.setNum(product1.getNum() + 1);
            productMapper.update(product, proQueryWrapper);

            // 转换相对路径为完整的图片地址
            String convertedContent = convertRelativePathToFullUrl(product1.getContents());
            product1.setContents(convertedContent);

            return Result.ok().data("data", product1);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error().message("error");
        }
    }

}
